package com.acme.bankapp.service.bank;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

import com.acme.bankapp.domain.bank.Bank;
import com.acme.bankapp.domain.bank.Client;
import com.acme.bankapp.domain.bank.Gender;
import com.acme.bankapp.exceptions.bank.ClientExistsException;


public class BankDataLoaderService {

	public static Client createClientFromString(String str){
		Client cl;
		Gender gender;
		str = str.replaceAll("[a-z]+=", "");
		String[] mass = str.split(";");

		if (!(mass.length == 4 || mass.length == 5)) {
			throw new IllegalArgumentException("Number of arguments are 4 or 5");
		}
		if (mass[mass.length - 1].equals("f")) {
			gender = Gender.FEMALE;
		} else if (mass[mass.length - 1].equals("m")) {
			gender = Gender.MALE;
		} else
			throw new IllegalArgumentException("Wrong format data for gender");

		if (mass[0].equals("c")) {
			cl = new Client(mass[3], gender, (int) Double.parseDouble(mass[1]),
					(int) Double.parseDouble(mass[2]));
		} else if (mass[0].equals("s")) {
			cl = new Client(mass[2], gender, (int) Double.parseDouble(mass[1]));
		} else
			throw new IllegalArgumentException("Wrong format data for account");

		return cl;
	}

	public static ArrayList<Client> clientArrayList(String file){
		ArrayList<String> str = new ArrayList<>();
		ArrayList<Client> clientsArray = new ArrayList<>();
		File f = new File(file);
		try {
			BufferedReader buf = new BufferedReader(new FileReader(f));
			Scanner scanner = new Scanner(buf);
			scanner.useDelimiter("\n");
			while (scanner.hasNext()) {
				str.add(scanner.next().trim());
			}
			for (String s : str) {
				clientsArray.add(createClientFromString(s));
			}
			scanner.close();

		} catch (IOException e) {
		}
		return clientsArray;
	}

	public static void main(String[] args) {
	Bank b= new Bank(clientArrayList("C:\\File.txt"));
	BankService.printBank(b);
	}
}
